using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using Peradigm.Framework.Domain.Parties;

using Liberty.Domain.ApplicationManagement;
using Liberty.Domain.CaseManagement;
using Liberty.Domain.ServiceMemberManagement;
using Liberty.Persistence.CaseManagement;
using Liberty.Persistence.Kernel;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests.PersistenceTests.CaseManagement
{
    [TestClass()]
    public class CaseBrokerTests
    {
        #region Properties

        protected string ConnectionString
        {
            get
            {
                return ConfigurationSettings.AppSettings[CONNECTION_STRING];
            }
        }

        #endregion

        #region Setup and Teardown
        [TestInitialize()]
        public void SetupCaseBrokerTests()
        {
        }

        [TestCleanup()]
        public void TeardownCaseBrokerTests()
        {
        }
        #endregion

        #region Test Methods

        [TestMethod()]
        public void TestCaseWith()
        {
            CaseBroker broker = new CaseBroker( this.ConnectionString );
            Case myCase = broker.CaseWith( TEST_CASE_ID );
            Assert.AreEqual( TEST_CASE_ID, myCase.Oid );
            List<Injury> injuries = new List<Injury>( myCase.Injuries );
            Assert.IsTrue( injuries.Count > 0 );
            List<Visit> visits = new List<Visit>( myCase.Visits );
            Assert.IsTrue( visits.Count > 0 );
        }

        [TestMethod()]
        public void TestCasesForServiceMember()
        {
            CaseBroker broker = new CaseBroker( this.ConnectionString );
            PersonBroker personBroker = new PersonBroker( this.ConnectionString );
            ServiceMember serviceMember = personBroker.ServiceMemberWith( TEST_SERVICE_MEMBER_ID );
            List<Case> cases = broker.CasesFor( serviceMember );
            Assert.IsTrue( cases.Count > 0 );
        }

        [TestMethod()]
        public void TestSaveCase()
        {
            try
            {
                CaseBroker broker = new CaseBroker( this.NewTransaction );

                Case myCase = broker.CaseWith( TEST_CASE_ID );
                Assert.AreEqual( TEST_CASE_ID, myCase.Oid );
                broker.Save( myCase );

                myCase = broker.CaseWith( TEST_CASE_ID );
                Assert.AreEqual( TEST_CASE_ID, myCase.Oid );
                List<Injury> injuries = new List<Injury>( myCase.Injuries );
                Assert.IsTrue( injuries.Count > 0 );
                List<Visit> visits = new List<Visit>( myCase.Visits );
                Assert.IsTrue( visits.Count > 0 );
            }
            finally
            {
                this.Rollback();
            }
        }

        #endregion

        #region Support Methods
        private SqlConnection CurrentConnection
        {
            get
            {
                if( i_cxn == null )
                {
                    i_cxn = new SqlConnection( this.ConnectionString );
                    i_cxn.Open();
                }
                return i_cxn;
            }
        }

        private SqlTransaction NewTransaction
        {
            get
            {
                if( i_txn == null )
                {
                    i_txn = this.CurrentConnection.BeginTransaction();
                }
                return i_txn;
            }
        }

        private void Rollback()
        {
            if( i_txn != null )
            {
                i_txn.Rollback();
                i_txn.Dispose();
                i_txn = null;
            }
        }

        private void Commit()
        {
            if( i_txn != null )
            {
                i_txn.Commit();
                i_txn.Dispose();
                i_txn = null;
            }
        }

        private void CloseConnection()
        {
            if( i_cxn != null )
            {
                this.Rollback();
                i_cxn.Close();
                i_cxn.Dispose();
                i_cxn = null;
            }
        }
        #endregion

        #region Data Elements
        private SqlConnection i_cxn;
        private SqlTransaction i_txn;
        #endregion

        #region Constants

        private const string CONNECTION_STRING = "ConnectionString";

        private const int TEST_CASE_ID = 5;
        private const int TEST_SERVICE_MEMBER_ID = 3;

        #endregion
    }
}
